﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using SecurityApi.DataAccess;
using SecurityAdmin.Web.Models;

namespace SecurityAdmin.Web.Controllers
{
    public class UserClaimController : BaseController
    {
        public ActionResult ManageClaims()
        {
            return View();
        }

        public ActionResult AddNewClaimsToUser()
        {
            AddUserClaimViewModel vm = new AddUserClaimViewModel();
            if (null != Session["SELECTED-RESOURCE"])
            {
                vm.ResourceId = (int)Session["SELECTED-RESOURCE"];
            }
            return View(vm);
        }

        [HttpPost]
        public ActionResult AddNewClaimsToUser(AddUserClaimViewModel vm)
        {
            int resourceId = vm.ResourceId;
            Session["SELECTED-RESOURCE"] = resourceId;
            return View();
        }

        public ActionResult AjaxGetAssignedClaims(int userId, [DataSourceRequest] DataSourceRequest request)
        {
            UserClaimRepository roleRepository = new UserClaimRepository();
            ClaimTypeRepository claimTypeRepository = new ClaimTypeRepository();
            ResourceRepository resourceRepository = new ResourceRepository();

            List<UserClaim> userClaims = roleRepository.GetUserClaims(userId).ToList();
            foreach (UserClaim uc in userClaims)
            {
                uc.Resource = resourceRepository.GetResourceById(uc.ResourceId);
                uc.ClaimType = claimTypeRepository.GetClaimTypeById(uc.ClaimTypeId);
            }

            return Json(userClaims.ToDataSourceResult(request));
        }


        public ActionResult AjaxGetAvailableClaims(int userId, [DataSourceRequest] DataSourceRequest request)
        {
            int resourceId = (int)Session["SELECTED-RESOURCE"];
            SecurityApiEntities context = new SecurityApiEntities();
            UserClaimRepository roleRepository = new UserClaimRepository(context);
            ClaimTypeRepository claimTypeRepository = new ClaimTypeRepository(context);
            ResourceRepository  resourceRepository = new ResourceRepository();

            IQueryable<ClaimType> claimTypes = claimTypeRepository.GetClaimTypes(SelectedApplication.ApplicationId);
            IQueryable<Resource> resources = resourceRepository.GetResources(SelectedApplication.ApplicationId);

            IQueryable<UserClaim> assignedUserClaims = roleRepository.GetUserClaims(userId);

            var resultList = from ct in claimTypes
                             where !(assignedUserClaims.Any(entry => entry.ClaimTypeId == ct.ClaimTypeId && entry.ResourceId == resourceId) )
                             select ct;


            return Json(resultList.ToDataSourceResult(request));
        }


        public ActionResult AddUserClaim(int claimTypeId)
        {
            int resourceId = (int)Session["SELECTED-RESOURCE"];
            UserClaimRepository roleRepository = new UserClaimRepository();
            roleRepository.AddUserClaim(SelectedUser.UserId, claimTypeId, resourceId);
            return RedirectToAction("AddNewClaimsToUser");
        }

        public ActionResult RemoveUserClaim(int userClaimId)
        {
            UserClaimRepository roleRepository = new UserClaimRepository();
            roleRepository.RemoveUserClaim(userClaimId);
            return RedirectToAction("ManageClaims");
        }

        
    }
}
